package com.dai.bean;

import com.dai.bean.tasks.*;
import org.json.JSONObject;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.time.LocalDate;
import java.util.*;

public class Task {

    public int user_id = -1;
    public String userdata = "";
    public String refresh_daytime = "";

    public static final Map<String,Class> clzMap = new HashMap<>();
    static {
        clzMap.put(TaskZaoWan.taskType.getValue(),TaskZaoWan.class);
        clzMap.put(TaskHeShui.taskType.getValue(),TaskHeShui.class);
        clzMap.put(TaskChiFan.taskType.getValue(),TaskChiFan.class);
        clzMap.put(TaskMeiRiYunDong.taskType.getValue(),TaskMeiRiYunDong.class);
    }

    public static  Task makeTask(int user_id){
        Task task = new Task();
        task.user_id = user_id;
        task.refresh_daytime = LocalDate.now().toString();
        JSONObject jsonObject = new JSONObject();
        try {
            for(String key:clzMap.keySet()){
                TaskBase taskBase = (TaskBase) clzMap.get(key).newInstance();
                jsonObject.put(taskBase.getTaskTypeName(),taskBase.toJsonObject());
            }
            task.userdata = jsonObject.toString();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        return task;
    }

    public static <T> T get(Class<T> clz, JSONObject obj){
        T t = null;
        try{
            Field[] fields = clz.getDeclaredFields();
            t = clz.newInstance();
            for(Field field:fields){
                int modifiers = field.getModifiers();
                // 只取非 static 变量
                if (!Modifier.isStatic(modifiers)){
                    System.out.println("name:" + field.getName());
                    Class fieldClz = field.getType();
                    if(fieldClz == Integer.class || fieldClz == int.class){
                        field.set(t,obj.getInt(field.getName()));
                    }else if(fieldClz == Float.class || fieldClz == float.class){
                        field.set(t,Float.valueOf(obj.get(field.getName()).toString()));
                    }else if(fieldClz == String.class){
                        field.set(t,obj.getString(field.getName()));
                    }else if(fieldClz == Long.class || fieldClz == long.class){
                        field.set(t,obj.getLong(field.getName()));
                    }
                    else{
                        throw new RuntimeException("还未支持的数据类型...." + fieldClz);
                    }
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        return  t;
    }


    public void reset(){

        try{
            JSONObject userdataJsonObject = new JSONObject(userdata);

            // 先全部检查更新
            List<TaskBase> bastLst = new ArrayList<>();
            for(String key:clzMap.keySet()){
                JSONObject obj = userdataJsonObject.getJSONObject(key);
                if(null == obj){
                    // 可能是新添加的
                    Class clz = clzMap.get(key);
                    TaskBase taskBase = (TaskBase) clz.newInstance();
                    bastLst.add(taskBase);
                }else{
                    TaskBase taskBase = (TaskBase) Task.get(clzMap.get(key),obj);
                    taskBase.checkReset();
                    bastLst.add(taskBase);
                }
            }

            // 再组装起来
            JSONObject jsonObject = new JSONObject();
            for(TaskBase taskBase:bastLst){
                jsonObject.put(taskBase.getTaskTypeName(),taskBase.toJsonObject());
            }
            userdata = jsonObject.toString();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }

    public void updateUserData(TaskZaoWan taskZaoWan){
        JSONObject userdataJsonObject = new JSONObject(userdata);
        userdataJsonObject.put(taskZaoWan.getTaskTypeName(),taskZaoWan.toJsonObject());
        userdata = userdataJsonObject.toString();
    }
    public void updateUserData(TaskHeShui taskHeShui){
        JSONObject userdataJsonObject = new JSONObject(userdata);
        userdataJsonObject.put(taskHeShui.getTaskTypeName(),taskHeShui.toJsonObject());
        userdata = userdataJsonObject.toString();
    }
}
